package com.bdqn.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bdqn.bus.dao.CarMapper;
import com.bdqn.bus.dao.RentMapper;
import com.bdqn.bus.entity.Car;
import com.bdqn.bus.entity.Check;
import com.bdqn.bus.dao.CheckMapper;
import com.bdqn.bus.entity.Rent;
import com.bdqn.bus.service.CheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.common.utitls.SystemConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KazuGin
 * @since 2020-05-15
 */
@Service
@Transactional
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements CheckService {

    @Resource
    private  CheckMapper checkMapper;

    @Resource
    private CarMapper carMapper;

    @Resource
    private RentMapper rentmap;

    @Override
    public boolean addCheck(Check check, Integer carId) {
        try {
            //设置检查单的创建事件
            check.setCreatetime(new Date());
            //调用保存检查单的方法
            if(checkMapper.insert(check)>0){
                //修改出租单的信息
                Rent rent=new Rent();
                rent.setRentflag(SystemConstants.RENT_FLAG_TRUE);//已归还
                rent.setReturndate(new Date());//还车时间
                UpdateWrapper<Rent> updateWrapper=new UpdateWrapper<Rent>();
                updateWrapper.eq("rentid",check.getRentid());//出租单号
                rentmap.update(rent,updateWrapper);
                //修改车辆信息
                Car car=new Car();
                car.setId(carId);//主键修改
                car.setIsrenting(SystemConstants.ISRENTING_FALSE);//未出租
                carMapper.updateById(car);
                return  true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
